package org.tgcloud.group.service.manager;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.multipart.MultipartFile;
import org.tgcloud.group.core.APPResultMessage;
import org.tgcloud.group.core.ExportUtils.ExcelUtils;
import org.tgcloud.group.core.ResultMessage;
import org.tgcloud.group.core.StringUtil;
import org.tgcloud.group.entity.MjPayroll;
import org.tgcloud.group.repository.MjPayrollMapper;
import org.tgcloud.group.web.controller.dto.PayRoll.PayRollImportDTO;
import org.tgcloud.group.web.controller.dto.PayRoll.PayRollListDTO;
import org.tgcloud.group.web.controller.dto.PayRoll.PayRollMsgDTO;
import org.tgcloud.group.web.controller.dto.file.ProjectHistoryDTO;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author: zhy
 * @create: 2023-10-30 09:14
 * @description:
 */
@Service
public class PayRollManageService {

    @Autowired
    private MjPayrollMapper payrollMapper;

    public APPResultMessage doImportExcelProjectHistoryDTO(MultipartFile file) {
        try {
            String fileName = StringUtil.generateOrderIdByName("CHECK");
            List<ProjectHistoryDTO> dtos = ExcelUtils.readMultipartFile(file, ProjectHistoryDTO.class, fileName,null ,null);
            return APPResultMessage.getSuccessMessage();
        }catch (Exception e){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new APPResultMessage(5001, e.getMessage());
        }
    }

    public APPResultMessage doImportExcelPayRoll(MultipartFile file, Date date) {
        try {
            MjPayroll mjPayroll = payrollMapper.selectMonthMsgByDate(date);
            if (mjPayroll != null){
                return APPResultMessage.getErrorMessage("该月已有对应工资表，请核实后进行修改月份或删除处理");
            }
            String fileName = StringUtil.generateOrderIdByName("CHECK");
            List<PayRollImportDTO> dtos = ExcelUtils.readMultipartFile(file, PayRollImportDTO.class, fileName,null ,null);
            for (PayRollImportDTO dto : dtos){
                MjPayroll payroll = new MjPayroll();
                payroll.setUsername(dto.getUsername());
                payroll.setArea(dto.getArea());
                payroll.setStatus((short) 1);
                payroll.setPayPrice(dto.getPayPrice());
                payroll.setCreateTime(new Date());
                payroll.setPayTime(date);
                payroll.setUserTypeName(dto.getUserTypeName());
                payroll.setUserType(dto.getUserTypeName().equals("商务") ? (short) 1 : (short) 10);
                payrollMapper.insertSelective(payroll);
            }
            return APPResultMessage.getSuccessMessage();
        }catch (Exception e){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new APPResultMessage(5001, e.getMessage());
        }
    }

    public APPResultMessage getPayRollList(Integer pageNum, Integer pageSize, Date time) {
        if (pageNum != null && pageSize != null){
            PageHelper.startPage(pageNum, pageSize);
            List<PayRollListDTO> list = payrollMapper.selectListByTime(time);
            PageInfo<PayRollListDTO> tables = new PageInfo<>(list);
            ResultMessage result = ResultMessage.builder().setList(tables.getList()).setAllPageNumber(tables.getPages()).setCount(tables.getTotal());
            APPResultMessage message = APPResultMessage.getSuccessMessage();
            message.setResult(result);
            return message;
        }else {
            List<PayRollListDTO> list = payrollMapper.selectListByTime(time);
            APPResultMessage successMessage = APPResultMessage.getSuccessMessage();
            ResultMessage resultMessage = ResultMessage.builder().setList(list);
            successMessage.setResult(resultMessage);
            return successMessage;
        }
    }

    public APPResultMessage getPayRollMonthList(Integer pageNum, Integer pageSize, Date time, Short userType, String userTypeName) {
        if (pageNum != null && pageSize != null){
            PageHelper.startPage(pageNum, pageSize);
            List<PayRollMsgDTO> list = payrollMapper.selectMonthListByTime(time, userType, userTypeName);
            PageInfo<PayRollMsgDTO> tables = new PageInfo<>(list);
            ResultMessage result = ResultMessage.builder().setList(tables.getList()).setAllPageNumber(tables.getPages()).setCount(tables.getTotal());
            APPResultMessage message = APPResultMessage.getSuccessMessage();
            message.setResult(result);
            return message;
        }else {
            List<PayRollMsgDTO> list = payrollMapper.selectMonthListByTime(time, userType, userTypeName);
            APPResultMessage successMessage = APPResultMessage.getSuccessMessage();
            ResultMessage resultMessage = ResultMessage.builder().setList(list);
            successMessage.setResult(resultMessage);
            return successMessage;
        }
    }

    public APPResultMessage getPayRollMonthExportList(Date time) {
        List<PayRollImportDTO> list = payrollMapper.selectMonthExportListByTime(time);
        APPResultMessage successMessage = APPResultMessage.getSuccessMessage();
        ResultMessage resultMessage = ResultMessage.builder().setList(list);
        successMessage.setResult(resultMessage);
        return successMessage;
    }

    public APPResultMessage doDelPayRollMonth(Date date) {
        payrollMapper.updateStatusByDate(date, (short) 10);
        return APPResultMessage.getSuccessMessage();
    }
}
